import { system, world } from "@minecraft/server";
import { GameComponent } from "@sapi-game/main";
import { PillarOFFortunePlayer } from "../module";
import { PillarOfFortuneMainState } from "../states/mainState";

export class PillarOfFortuneDamage extends GameComponent<PillarOfFortuneMainState> {
    override onAttach(): void {
        this.subscribe(world.afterEvents.entityHurt, (t) => {
            if (!t.damageSource.damagingEntity) return;
            const player = this.context.players.getById(t.hurtEntity.id);
            const source = this.context.players.getById(
                t.damageSource.damagingEntity.id
            );
            if (!player || !source) return;
            player.lastDamagePlayer = source;
            player.lastDamageTick = system.currentTick;
        });
    }

    getSource(player: PillarOFFortunePlayer) {
        if (
            player.lastDamagePlayer &&
            system.currentTick - player.lastDamageTick < 100
        ) {
            return player.lastDamagePlayer;
        }
    }
}
